﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Pes.Messaging.DatabaseLayer
{
    public static class Texts
    {
        public static bool FlagSent(string id)
        {
            string sql = "UPDATE dbo.OutgoingTexts SET Sent=1 WHERE OutgoingTextId=@OutgoingTextId;";
            using (SqlCommand command = new SqlCommand(sql))
            {
                command.Parameters.AddWithValue("@OutgoingTextId", id);
                return Pes.DBFactory.DBOperations.ExecuteNonQuery(command);
            }
        }

        public static DataTable GetTextQueue()
        {
            string sql = "SELECT * FROM dbo.OutgoingTextView WHERE Sent = 0;";
            using (SqlCommand command = new SqlCommand(sql))
            {
                return Pes.DBFactory.DBOperations.GetDataTable(command);
            }
        }

        public static bool SaveMessage(string cellNumber, string message)
        {
            string sql = "INSERT INTO dbo.IncomingTexts(CellNumber, Message) SELECT @CellNumber, @Message;";
            using (SqlCommand command = new SqlCommand(sql))
            {
                command.Parameters.AddWithValue("@CellNumber", cellNumber);
                command.Parameters.AddWithValue("@Message", message);

                Pes.Messaging.DatabaseLayer.Emails.SaveToQueue(ConfigurationManager.AppSettings["AdminEmailAddresses"], "SMS from " + cellNumber, message);

                return Pes.DBFactory.DBOperations.ExecuteNonQuery(command);
            }
        }
    }
}
